//
//  QSClient.m
//  QS
//
//  Created by jonn on 14-3-18.
//  Copyright (c) 2014年 seas.cheng. All rights reserved.
//

#import "QTClient.h"
#import "Persistence.h"
#import "QTProductPersistence.h"

@implementation QSClient

static QSClient *_QSClient;
static QSClient *_PrivateQSClient;
static QSClient *_QSClientWifi;

+ (QSClient *) instance {
    if ( _QSClient==nil) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken,^{
            _QSClient=[[QSClient alloc] init ];
        });
    }
    return _QSClient;
}


- (id)init {
    return [super initWithBaseURL:[NSURL URLWithString:qsServerUrl]];
}

- (id)initWithBaseURL:(NSURL *)url {
    return [super initWithBaseURL:url];
}

// 用户登录
- (void)login:(NSString *)loginName loginPassword:(NSString *)loginPassword  completionBlock:(void (^)(User *user, NSError *error))block
{
    
    NSString *url=[NSString stringWithFormat:@"appLoginAction_userLogin.action?loginName=%@&loginPassword=%@",loginName,loginPassword,nil];
    NSLog(@"url:%@",url);
    [self POST:url parameters:NULL resultClass:User.class resultKeyPath:nil completion:^(AFHTTPRequestOperation *operation, id responseObject, NSError *error) {
        NSLog(@"responseObject:%@",responseObject);
        block(responseObject, error);
    }];
}

// 个人中心信息
-(void)loadUserInfo:(NSString *)puid completionBlock:(void (^)(UserInfo *, NSError *))block
{
    
    NSString *url=[NSString stringWithFormat:@"appBasicUserAction_findUserInfo.action?puid=%@",puid,nil];
    NSLog(@"url:%@",url);
    [self POST:url parameters:NULL resultClass:UserInfo.class resultKeyPath:nil completion:^(AFHTTPRequestOperation *operation, id responseObject, NSError *error) {
        NSLog(@"responseObject:%@",responseObject);
        block(responseObject, error);
    }];
}

// 产品信息列表
- (void)loadProductList:(NSNumber *)startIndex maxResults:(NSNumber *)maxResults productName:(NSString *)productName manufacturerName:(NSString *)manufacturerName productType:(NSString *)productType brand:(NSString *)brand keyWord:(NSString *)keyWord completionBlock:(void (^)(ProductList *productList, NSError *error))block
{
    NSString *url=[NSString stringWithFormat:@"appProductInfoAction_productSearchList.action?startindex=%@&maxresults=%@&productName=%@&manufacturerName=%@&productType=%@&brand=%@&keyWord=%@",startIndex,maxResults,productName,manufacturerName,productType,brand,keyWord,nil];
    NSString *encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"encodedUrl:%@",encodedUrl);
    [self POST:encodedUrl parameters:NULL resultClass:ProductList.class resultKeyPath:nil completion:^(AFHTTPRequestOperation *operation, id responseObject, NSError *error) {
        NSLog(@"responseObject:%@",responseObject);
        block(responseObject, error);
    }];
}



// 产品详情
- (void)loadProductDetail:(NSString *)puid barCode:(NSString *)barCode completionBlock:(void (^)(ProductDetail *productDetail, NSError *error))block
{
    NSString *url=[NSString stringWithFormat:@"appProductInfoDetailAction_productSearchListById.action?puid=%@&barCode=%@",puid,barCode,nil];
    NSLog(@"url:%@",url);
    [self POST:url parameters:NULL resultClass:ProductDetail.class resultKeyPath:nil completion:^(AFHTTPRequestOperation *operation, id responseObject, NSError *error) {
        NSLog(@"responseObject:%@",responseObject);
        block(responseObject, error);
    }];
}

// 产品评价
- (void)loadEvaluationInfo:(NSString *)puid evaluationTypeId:(NSString *)evaluationTypeId startIndex:(NSNumber *)startIndex maxResults:(NSNumber *)maxResults completionBlock:(void (^)(ProductEvaluation *productEvaluation, NSError *error))block
{
    
    NSString *url=[NSString stringWithFormat:@"appProductEvaluationInfo_appEvaluationInfoSearch.action?puid=%@&evaluationTypeId=%@&startIndex=%@&maxResults=%@",puid,evaluationTypeId,startIndex,maxResults,nil];
    NSLog(@"url:%@",url);
    [self POST:url parameters:NULL resultClass:ProductEvaluation.class resultKeyPath:nil completion:^(AFHTTPRequestOperation *operation, id responseObject, NSError *error) {
        NSLog(@"responseObject:%@",responseObject);
        block(responseObject, error);
    }];
}

// 资讯列表（新闻列表/抽检报告列表）

- (void)loadNewsList:(NSNumber *)articleMenuType startIndex:(NSNumber *)startIndex maxResults:(NSNumber *)maxResults productTypePuid :(NSString *) productTypePuid completionBlock:(void (^)(NSArray *articleList, NSError *error))block;

{
    NSString *url=[NSString stringWithFormat:@"appAction_getArticleByMenuWithoutContent.action?articleMenuType=%@&startIndex=%@&maxResults=%@&productTypePuid=%@",articleMenuType, startIndex, maxResults, productTypePuid, nil];
    NSLog(@"url:%@",url);
    [self POST:url parameters:NULL resultClass:QTArticle.class resultKeyPath:nil completion:^(AFHTTPRequestOperation *operation, id responseObject, NSError *error) {
        NSLog(@"responseObject:%@",responseObject);
        block(responseObject, error);
    }];
}

// 资讯详情（新闻详情）
- (void)loadNewsDetail:(NSString *)articleId completionBlock:(void (^)(ArticleDetail *articleDetail, NSError *error))block
{

    NSString *url=[NSString stringWithFormat:@"appAction_getArticleById.action?articleId=%@",articleId,nil];
    NSLog(@"url:%@",url);
    [self POST:url parameters:NULL resultClass:ArticleDetail.class resultKeyPath:nil completion:^(AFHTTPRequestOperation *operation, id responseObject, NSError *error) {
        NSLog(@"responseObject:%@",responseObject);
        block(responseObject, error);
    }];
}

// 商品企业信息查询
- (void)findEnterpriseInfoById:(NSString *)puid completionBlock:(void (^)(EnterpriseInfo *enterpriseInfo, NSError *error))block
{
    
    NSString *url=[NSString stringWithFormat:@"appEnterprise_findEnterpriseInfoById.action?puid=%@",puid,nil];
    NSLog(@"url:%@",url);
    [self POST:url parameters:NULL resultClass:EnterpriseInfo.class resultKeyPath:nil completion:^(AFHTTPRequestOperation *operation, id responseObject, NSError *error) {
        NSLog(@"responseObject:%@",responseObject);
        block(responseObject, error);
    }];
}

// 分类搜索
- (void)searchWithCategory:(NSString *)flag completionBlock:(void (^)(QTSearchCategory *SearchCategory, NSError *error))block;
{
    NSString *url=[NSString stringWithFormat:@"appClassificationAction_geetClassificationList.action?%@",flag];
    NSLog(@"url:%@",url);
    [self POST:url parameters:NULL resultClass:QTSearchCategory.class resultKeyPath:nil completion:^(AFHTTPRequestOperation *operation, id responseObject, NSError *error) {
        NSLog(@"responseObject:%@",responseObject);
        block(responseObject, error);
    }];
}


//主页广告
- (void)mainPagePic:(void (^)(QTDetailCategory *DetailCategory, NSError *error))block;
{
    NSString *url=[NSString stringWithFormat:@"appIndexFlashAction_findAllFlashImage.action"];
    NSLog(@"url:%@",url);
    [self POST:url parameters:NULL resultClass:QTDetailCategory.class resultKeyPath:nil completion:^(AFHTTPRequestOperation *operation, id responseObject, NSError *error) {
        NSLog(@"responseObject:%@",responseObject);
        block(responseObject, error);
    }];
}

//.关于我们
-(void)loadAboutUsInfocompletionBlock:(void (^)( AboutUsInfo *aboutUsInfo, NSError *error))block;
{
    NSString *url=[NSString stringWithFormat:@"appAboutUsAction_findAll.action"];
    NSLog(@"url:%@",url);
    [self POST:url parameters:NULL resultClass:AboutUsInfo.class resultKeyPath:nil completion:^(AFHTTPRequestOperation *operation, id responseObject, NSError *error) {
        NSLog(@"responseObject:%@",responseObject);
        block(responseObject, error);
    }];
}

//.摇一摇
- (void)getSearchLabels:(NSString *)parameters completionBlock:(void (^)(ProductRandomLabel *productRandomLabel, NSError *error))block
{
    NSString *url=[NSString stringWithFormat:@"appLableAction_geetProductLableListRandom.action"];
    
    [self POST:url parameters:NULL resultClass:ProductRandomLabel.class resultKeyPath:nil completion:^(AFHTTPRequestOperation *operation, id responseObject, NSError *error) {
        block(responseObject, error);
    }];
}

@end
